Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Common
Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Repositories

Public Class EditGoalRating

    Public Event GoalRatingUpdated(ByVal goalRating As GoalRating)


    Public Sub New(ByVal performanceEvaluationId As Guid, ByVal goalId As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.PerformanceEvaluationId = performanceEvaluationId
        Me.GoalId = goalId

        LoadSelectableProperties()

        Dim performanceEvaluationRepository As New PerformanceEvaluationRepository()

        Dim performanceEvaluation As PerformanceEvaluation = performanceEvaluationRepository.GetById(performanceEvaluationId)

        Dim goalRating As GoalRating = performanceEvaluation.GetGoalRating(goalId)

        If goalRating IsNot Nothing Then
            Me.GoalName = goalRating.Goal.Name
            Me.SelectedRatingLevel = goalRating.RatingLevel
        End If

    End Sub

    Private _performanceEvaluationId As Guid
    Public Property PerformanceEvaluationId() As Guid
        Get
            Return _performanceEvaluationId
        End Get
        Set(ByVal value As Guid)
            _performanceEvaluationId = value
        End Set
    End Property

    Public Property GoalName() As String
        Get
            Return lblGoal.Text
        End Get
        Set(ByVal value As String)
            lblGoal.Text = value
        End Set
    End Property

    Public Property SelectedRatingLevel() As RatingLevel
        Get
            Return CType(cmbRatingLevels.SelectedValue, RatingLevel)
        End Get
        Set(ByVal value As RatingLevel)
            cmbRatingLevels.SelectedValue = CInt(value)
        End Set
    End Property

    Private _goalId As Guid
    Public Property GoalId() As Guid
        Get
            Return _goalId
        End Get
        Set(ByVal value As Guid)
            _goalId = value
        End Set
    End Property

    Public Sub AddRatingLevels(ByVal levels As IList(Of Pair(Of Integer, String)))

        cmbRatingLevels.DataSource = levels
        cmbRatingLevels.ValueMember = "Arg1"
        cmbRatingLevels.DisplayMember = "Arg2"

    End Sub

    Public Sub RaiseGoalRatingUpdatedEvent(ByVal rating As GoalRating)
        RaiseEvent GoalRatingUpdated(rating)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Me.UpdateGoalRating()
        Me.Close()

    End Sub

    Private Sub LoadSelectableProperties()

        Dim ratingLevels As IList(Of Pair(Of Integer, String)) = RatingLevelExtension.GetValues()

        Me.AddRatingLevels(ratingLevels)

    End Sub

    Public Sub UpdateGoalRating()


        Dim performanceEvaluationRepository As New PerformanceEvaluationRepository()

        Dim performanceEvaluation As PerformanceEvaluation = performanceEvaluationRepository.GetById(Me.PerformanceEvaluationId)

        Dim goalRating As GoalRating = performanceEvaluation.GetGoalRating(Me.GoalId)

        goalRating.RatingLevel = Me.SelectedRatingLevel

        performanceEvaluationRepository.SaveOrUpdateGoalRating(goalRating)

        Me.RaiseGoalRatingUpdatedEvent(goalRating)


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

End Class